<?php

namespace App\V3\Api\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class PackageModel extends Model
{
    use HasFactory;

    protected $table = 'package';
    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储

    //toArray 时间转换问题
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function hasManyContent()
    {
        return SELF::hasMany(PackageContentModel::class, 'package_id', 'id');
    }

    //检索套餐名称信息
    static public function getPackageInfo($keyword, $data)
    {
        return SELF::select(
            'id'
        )
            ->where('package_name', 'LIKE', '%'.$keyword.'%')
            ->where('main_id', $data['main_id'])
            ->where('delete_flag', 0)->get()->toArray();
    }

    static public function withSearch($data, $arr)
    {
        $query = SELF::select(
            'id',
            'package_name',
            'status'
        )
            ->where('delete_flag', 0)
            ->with('hasManyContent', function($query) {
                $query->select(
                    'package_content.id',
                    'materiel_first_class_id',
                    'a.class_name as materiel_first_class_name',
                    'a.class_no as materiel_first_class_no',
                    'package_content.materiel_class_id',
                    'b.class_name as materiel_class_name',
                    'b.class_no as materiel_class_no',
                    'package_content.type_id',
                    'c.id as type_id',
                    'c.materiel_name',
                    'c.type_name',
                    'c.materiel_img',
                    'c.materiel_no',
                    'package_content.storage_id',
                    'd.storage_name',
                    'd.storage_address',
                    'storage_manage_id',
                    'package_id',
                    'num',
                    'e.id as storage_manage_id',
                    'e.in_storage_num',
                    'e.delete_flag',
                    'e.shelf_id',
                    'e.location_id',
                    's.shelf_name',
                    'l.location_name',
                    'c.cost',
                    'c.selling_price',
                    'c.is_use_serial',
                    'c.lower_selling_price'
                )
                    ->leftJoin('materiel_class as a', 'materiel_first_class_id', '=', 'a.id')
                    ->leftJoin('materiel_class as b', 'package_content.materiel_class_id', '=', 'b.id')
                    ->leftJoin('materiel_type as c', 'package_content.type_id', '=', 'c.id')
                    ->leftJoin('storage as d', 'd.id', '=', 'package_content.storage_id')
                    ->leftJoin('storage_manage as e', 'e.id', '=', 'package_content.storage_manage_id')
                    ->leftJoin('storage_shelf as s','e.shelf_id','=','s.id')
                    ->leftJoin('storage_shelf_location as l','e.location_id','=','l.id')
                    ->where('package_content.delete_flag', 0);
            })
            ->whereIn('id', $arr)
            ->orderBy('id', 'DESC')
            ->paginate($data['pageSize'],['*'], '',$data['currentPage']);

        return $query;
    }

    //无条件检索套餐及套餐内容
    static public function withoutSearch($data)
    {   $info = "";
        if(!empty($data['storage_id'])){
            $info = PackageContentModel::
            where(['main_id'=>$data['main_id'],'storage_id'=>$data['storage_id']])
            ->where('delete_flag',0)->first();
            if(empty($info))$info = "XJCK";
        }
         $query = SELF::select(
            'id',
            'package_name',
            'status'
        )
            ->where('main_id', $data['main_id'])
            ->where('delete_flag', 0)
            ->with('hasManyContent', function($query) {
                $query->select(
                    'package_content.id',
                    'materiel_first_class_id',
                    'a.class_name as materiel_first_class_name',
                    'a.class_no as materiel_first_class_no',
                    'package_content.materiel_class_id',
                    'b.class_name as materiel_class_name',
                    'b.class_no as materiel_class_no',
                    'd.id as type_id',
                    'd.materiel_name',
                    'd.type_name',
                    'd.materiel_no',
                    'package_content.storage_id',
                    'e.storage_name',
                    'e.storage_address',
                    'package_id',
                    'd.materiel_img',
                    'd.materiel_thumbnail_img',
                    'num',
                    'c.in_storage_num',
                    'c.id as storage_manage_id',
                    'c.delete_flag',
                    'c.shelf_id',
                    'c.location_id',
                    's.shelf_name',
                    'l.location_name',
                    'd.cost',
                    'd.selling_price',
                    'd.is_use_serial',
                    'd.lower_selling_price'
                )
                    ->leftJoin('materiel_class as a', 'materiel_first_class_id', '=', 'a.id')
                    ->leftJoin('materiel_class as b', 'package_content.materiel_class_id', '=', 'b.id')
                    ->leftJoin('materiel_type as d', 'package_content.type_id', '=', 'd.id')
                    ->leftJoin('storage as e', 'e.id', '=', 'package_content.storage_id')
                    ->leftJoin('storage_manage as c', 'c.id', '=', 'storage_manage_id')
                    ->leftJoin('storage_shelf as s','c.shelf_id','=','s.id')
                    ->leftJoin('storage_shelf_location as l','c.location_id','=','l.id')
                    ->where('package_content.delete_flag', 0);                                                     
            })
            ->orderBy('id', 'DESC');           
            if($info=='XJCK')$query->where('id',"");
           return $query->paginate($data['pageSize'],['*'], '',$data['currentPage']);
           
        
    }

    //检查套餐信息是否存在
    static public function checkInfo($data, $mainId)
    {
        return SELF::where([
            'package_name' => $data['package_name'],
            'main_id' => $mainId,
            'delete_flag' => 0
        ])->first();
    }
}
